package com.exam.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.exam.exception.StudentNonExist;
import com.exam.mapper.ClassesMapper;
import com.exam.mapper.MajorMapper;
import com.exam.pojo.Classes;
import com.exam.pojo.Major;
import com.exam.pojo.Paging;
import com.exam.pojo.Student;
import com.exam.service.ClassesService;
import com.exam.service.MajorService;
import com.exam.service.StudentService;
import com.exam.util.SnoUtil;
import com.google.gson.Gson;

/**
 * @author 郭荣锋
  *  学生管理控制器
 */
@Controller
@RequestMapping("/StudentManage")
public class StudentManage {
	
	@Resource(name="studentServiceImpl")
	private StudentService studentService;
	@Resource(name="mv")
	private ModelAndView mv;
	@Resource(name="majorServiceImpl")
	private MajorService majorService;	
	@Resource(name="classesServiceImpl")
	private ClassesService classesService;
	@Resource(name="student")
	private Student student;
	@Resource(name="gson")
	private Gson gson;
	@Resource(name="map")
	private Map map;
	
	/**
	  *   学生列表视图
	 * @param
	 * @return
	 */
	@RequestMapping("/student-list")
	public ModelAndView studentList(HttpServletRequest request,HttpServletResponse response) {
		List<Major> majorList = majorService.getAllMajor(); //专业列表
		mv.addObject("majorList",majorList);
		mv.setViewName("admin/student-list");
		return mv;
	}

	/**
	 * 显示已删除的学生列表
	 * @return
	 */
	@RequestMapping("/student-deleteList")
	public ModelAndView student_delete(HttpServletRequest request,HttpServletResponse response) {
		List<Major> majorList = majorService.getAllMajor(); //专业列表
		mv.addObject("majorList",majorList);
		mv.setViewName("admin/student-deleteList");
		return mv;
	}
	/**
	 *  添加学生页面
	 * @param
	 * @return
	 */
	@RequestMapping("/student-add")
	public ModelAndView studentAdd(HttpServletRequest request,HttpServletResponse response) {
		
		//添加专业列表
		List<Major> majorList = majorService.getAllMajor(); //专业列表
		mv.addObject("majorList",majorList);
		mv.setViewName("admin/student-add");
		return mv;
	}
	
	/**
	 *  更新学生信息页面
	 * @param
	 * @return
	 */
	@RequestMapping("/student-update")
	public ModelAndView studentUpdate(HttpServletRequest request,HttpServletResponse response) {
		Integer studentId = Integer.parseInt(request.getParameter("studentId")); //学生编号
		Student student = studentService.getStudentById(studentId);
		//添加专业列表	
		List<Major> majorList = majorService.getAllMajor(); //专业列表
		mv.addObject("majorList",majorList);
		//填充名字和密码编号
		mv.addObject("studentId",studentId);
		mv.addObject("studentName",student.getStudentName());
		mv.addObject("password",student.getPassword());
		//定位专业下拉框和班级下拉框
		Major major = majorService.getMajorByName(student.getClasses().getMajor().getMajorName());
		mv.addObject("majorValue",major.getId());
		mv.addObject("classesValue",Integer.parseInt
				(SnoUtil.getClassesName(student.getSno()).substring(2,4)));
		mv.setViewName("admin/student-update");
		return mv;
	}
	
	/**
	 * 获取学生分页列表
	 * @param
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/getStudentByPaging")
	public void getStudentByPaging(HttpServletRequest request,HttpServletResponse response) throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
	    response.setCharacterEncoding("UTF-8");
		PrintWriter printWriter = response.getWriter();
		
		Integer page = Integer.parseInt(request.getParameter("page")); //当前页
		String classesName = request.getParameter("classesName"); //班级名称
		Integer pageSize = 10; //页面大小
		String majorName = request.getParameter("majorName");
		
		Map map = new HashMap();
		map.put("page",page);
		map.put("pageSize",pageSize);
		map.put("majorName",majorName);
		map.put("classesName",classesName);
		Paging<Student> pagingList = studentService.studentPaging(map);
		String s = gson.toJson(pagingList); 
		printWriter.write(s);
		printWriter.close();
	}
	
	/**
	 * 获取被删除的学生分页列表
	 * @param
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/getDeletedStudentByPaging")
	public void getDeletedStudentByPaging(HttpServletRequest request,HttpServletResponse response) throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
	    response.setCharacterEncoding("UTF-8");
		PrintWriter printWriter = response.getWriter();
		Integer page = Integer.parseInt(request.getParameter("page")); //当前页
		String classesName = request.getParameter("classesName"); //班级名称
		Integer pageSize = 10; //页面大小
		String majorName = request.getParameter("majorName");	
		Map map = new HashMap();
		map.put("page",page);
		map.put("pageSize",pageSize);
		map.put("majorName",majorName);
		map.put("classesName",classesName);
		Paging<Student> pagingList = studentService.getDeletedStudentByPaging(map);
		String s = gson.toJson(pagingList); 
		printWriter.write(s);
		printWriter.close();
	}
	
	/**
	 * 通过专业名称获取班级列表
	 * @param
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/getClassesByMajorName")
	public void getClassesByMajorName(HttpServletRequest request,HttpServletResponse response) throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
	    response.setCharacterEncoding("UTF-8");
		PrintWriter printWriter = response.getWriter();
		String majorName = request.getParameter("majorName");
		List<Classes> classesList = classesService.getClassesByMajorName(majorName);
		String list = gson.toJson(classesList); 
		printWriter.write(list);
		printWriter.close();
	}
	
	/**
	 *添加学生
	 * @param
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/addStudent")
	public void addStudent(HttpServletRequest request,HttpServletResponse response) throws IOException {
		
		PrintWriter printWriter = response.getWriter();
		String studentName = request.getParameter("studentName"); //学生姓名
		String password = request.getParameter("password"); //密码
		String majorName = request.getParameter("majorName"); //专业名称
		String classesName = request.getParameter("classesName"); //班级名称
		//把收到的数据封装好直接丢给服务层 
		map.clear();
		map.put("studentName",studentName);
		map.put("password",password);
		map.put("majorName",majorName);
		map.put("classesName",classesName);
		studentService.addStudent(map);
		printWriter.write("{\"res\":\"1\"}");
		printWriter.close();
	}
	
	/**
	 * 删除学生
	 * @param
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/deleteStudent")
	public void deteteStudent(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter printWriter = response.getWriter();
		Integer studentId = Integer.parseInt(request.getParameter("studentId"));
		Student student = studentService.getStudentById(studentId);
		System.out.println(studentId);
		try {
			studentService.deleteStudent(student);
			printWriter.write("{\"res\":\"1\"}");
		} catch (StudentNonExist e) {
			System.out.println("学生不存在");
			printWriter.write("{\"res\":\"0\"}");
		}
		printWriter.close();
	}
	
	/**
	 * 更新学生信息
	 * @param
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/updateStudent")
	public void updateStudent(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter printWriter = response.getWriter();
		Integer studentId = Integer.parseInt(request.getParameter("studentId")); //学生编号
		String studentName = request.getParameter("studentName");
		String majorName = request.getParameter("majorName");
		String classesName = request.getParameter("classesName");
		String password = request.getParameter("password");
		student.setId(studentId);
		Map map = new HashMap(); //封装好方法
		map.put("studentName",studentName);
		map.put("majorName",majorName);
		map.put("password",password);
		map.put("classesName",classesName);
		map.put("studentId",studentId);
		studentService.updateStudent(map);
		printWriter.write("{\"res\":\"1\"}");
		printWriter.close();
	}
	
	/**
	 * 批量删除学生
	 * @throws IOException 
	 */
	@RequestMapping("/deleteStudentByQuery")
	public void deleteStudentByQuery(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter printWriter = response.getWriter();
		String list = request.getParameter("studentIdList");
		String[] list1 = gson.fromJson(list,String[].class); //获取要删除的学生编号
		
		for(String studentId:list1) { //循环遍历删除
			Student student = studentService.getStudentById(Integer.parseInt(studentId));
			try {
				studentService.deleteStudent(student);
			} catch (StudentNonExist e) {
				e.printStackTrace();
			}
		}
		printWriter.write("{\"res\":\"1\"}");
		printWriter.close();
	}
	
	/**
	   *     恢复被删除的学生
	 * @throws IOException 
	 */
	@RequestMapping("/recoverStudent")
	public void recoverStudent(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter printWriter = response.getWriter();
		Integer studentId = Integer.parseInt(request.getParameter("studentId"));
		studentService.recoverStudent(studentId);
		printWriter.write("{\"res\":\"1\"}");
		printWriter.close();
	}
	
}
